﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Imaginecup.shareData;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;


namespace Imaginecup.util
{
    public interface IGamePadHandler
    {
        GamePadState PadState { get; }
        GamePadState PreviousPadState { get; }

        void SetTexture(Texture2D texture);
        bool IsOneDown(Buttons btns);
        bool IsOneUp(Buttons btns);
    }

    public class GamePadHandler : DrawableGameComponent, IGamePadHandler
    {
        private Texture2D m_SelectTexture;
        private Texture2D m_DefaultTexture;
        
        private GamePadState currentPadState;
        private GamePadState prevPadState;

        private ContentManager m_Content;

        private static GamePadHandler m_InputHandler = null;

        private GamePadHandler()
            : base(StateShareData.GetShareData().GetGame())
        {
            prevPadState = GamePad.GetState(PlayerIndex.One);

            m_Content = new ContentManager(Game.Services);
            m_Content.RootDirectory = "Content";

            this.DrawOrder = 1;
            base.Initialize();
        }


        public static GamePadHandler GetInputHandler()
        {
            if (m_InputHandler == null)
            {
                m_InputHandler = new GamePadHandler();
            }
            return m_InputHandler;
        }

        public void SetTexture(Texture2D texture)
        {
            m_SelectTexture = texture;
        }

        protected override void LoadContent()
        {
            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            m_SelectTexture = null;
            m_DefaultTexture.Dispose();
            m_DefaultTexture = null;

            m_Content.Unload();
            base.UnloadContent();
        }

        public bool IsOneDown(Buttons btns)
        {
            if (prevPadState.IsButtonUp(btns) && currentPadState.IsButtonDown(btns))
            {
                return true;
            }
            return false;
        }

        public bool IsOneUp(Buttons btns)
        {
            if (prevPadState.IsButtonDown(btns) && currentPadState.IsButtonUp(btns))
            {
                return true;
            }
            return false;
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sb = GraphicShareData.GetShareData().GetSpriteBatch();

            if( m_SelectTexture == null )
            {
                m_SelectTexture = m_DefaultTexture;
            }

            base.Draw(gameTime);
        }

        public sealed override void Update(GameTime gameTime)
        {
            prevPadState = currentPadState;
            currentPadState = GamePad.GetState(PlayerIndex.One);

            base.Update(gameTime);
        }

        public GamePadState PadState
        {
            get { return (currentPadState); }
        }
        public GamePadState PreviousPadState
        {
            get { return (prevPadState); }
        }
    }

    
}